<?php
/**
 * Created by PhpStorm.
 * Date: 15/9/15
 * Time: 下午3:46
 */

class PushMessageController extends BaseController {
    /*
     * 推送短信
     *
     * */
    public function pushMessage()
    {
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            $phone = Input::get('phone');
            $content = Input::get('content');
            //记录数据库
            $smsRecord = new SmsSendRecords;
            $smsRecord->area = '';
            $smsRecord->phone = $phone;
            $smsRecord->content = $content;
            $smsRecord->status = Sms::MESSAGE_SENDING;
            $smsRecord->created_at = Common::num2DateTime(time());
            $smsRecord->save();

            $id = $smsRecord->id;
            $result = Sms::send($phone,$content,$id);
            $result = substr($result->body,74,strlen($result->body) - 74 - 9);
            $montnets_error_code = Config::get("sms.montnets_error_code");
            if($result > 0 || $result < -20000){
                $update = array('status' => 1 , 'result_msg' => '发送成功');
                $update1 = array('status' => 1 , 'result_msg' => '发送成功');
            }else{
                $update = array('status' => -1 , 'result_msg' => $result . ' ' . $montnets_error_code[$result]);
                $update1 = array('status' => -1 , 'result_msg' =>$montnets_error_code[$result]);
            }
            SmsSendRecords::updateSmsRecord($id,$update);
            $response['result'] = $update1;
        }while (false);
        Log::info("info", array("system" => "report" ,"event" => "pushMessage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    } /*
     * 推送微信
     *
     * */
    public function pushWechat()
    {
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            $phone = Input::get('phone');
            $first = Input::get('first');
            $keynote1 = Input::get('keynote1');
            $keynote2 = Input::get('keynote2');
            $remark = Input::get('remark');
            $wechat_user=Users::where('phone',$phone)->first();
            if(!$wechat_user || !$wechat_user->openid){
                $response = array('code' => -1, 'msg' => 'fail', 'result' => array());
                break;
            }
            $templateId = Config::get('config.scs_wechat_push_ins_id');
            $data = array(
                "first"  => $first,
                "keyword1" => $keynote1,
                "keyword2"   => $keynote2,
                "remark" => $remark
            );
            try {
               $result = WechatOauthController::sendMessage($wechat_user->openid, $templateId, $data,'');
               $response['result'] = $result;
            }catch(Exception $e){
                $response = array('code' => -1, 'msg' => 'fail', 'result' => array());
                Log::error("error", array("system" => "scs" ,"event" => "sendMessage", 'input' =>Input::all() ,"exception" => $e));
            }
        }while (false);
        Log::info("info", array("system" => "report" ,"event" => "pushWechat", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}